<template>
    <view class="life-container">
        <!-- 固定头部区域 -->
        <view class="fixed-header">
            <!-- 顶部导航标签 -->
            <view class="nav-tabs">
                <!-- <text :class="['tab-item', activeTab === 'new' ? 'active' : '']" @click="switchTab('new')">最新</text> -->
                <text :class="['tab-item', activeTab === 'hot' ? 'active' : '']" @click="switchTab('hot')">热门
                    <view :style="{ backgroundColor: t('color1') }" class="active-curve"></view>
                </text>
                <!-- <text :class="['tab-item', activeTab === 'mark' ? 'active' : '']" @click="switchTab('mark')">收藏</text>
                <text :class="['tab-item', activeTab === 'follow' ? 'active' : '']"
                    @click="switchTab('follow')">关注</text> -->
            </view>

            <!-- <view @tap.stop="goto" :data-url="'/pagesZone/video/video_search?type=' + getType" class="search-container"
                v-if="activeTab != 'follow'">
                <view class="search-box">
                    <image class="img" src="@/static/img/search-icon2.png"></image>
                    <view class="search-text">请输入搜索内容</view>
                </view>
            </view> -->

            <!-- 筛选选项 -->
            <!-- <view class="filter-options">
                <text :class="['filter-item', activeFilter === 'latest' ? 'active' : '']"
                    @click="switchFilter('latest')">最新发布</text>
                <text :class="['filter-item', activeFilter === 'hot' ? 'active' : '']"
                    @click="switchFilter('hot')">热门推荐</text>
                <text :class="['filter-item', activeFilter === 'replies' ? 'active' : '']"
                    @click="switchFilter('replies')">最新回复</text>
            </view> -->
        </view>

        <!-- 可滚动的内容区域 -->
        <view class="content-container">
            <!-- <scroll-view class="scrollable-content" scroll-y="true"> -->
            <view class="content-grid" v-if="contentList.length > 0 && activeTab != 'follow'">
                <view class="content-card" v-for="(item, index) in contentList" :key="index" @tap="toDetail(item)">
                    <image class="card-image" :src="item.image" mode="aspectFill"></image>
                    <view class="card-info">
                        <text class="card-title">{{ item.name }}</text>
                        <view class="user-info">
                            <image class="user-avatar" :src="item.author_headimg" mode="aspectFill"></image>
                            <text class="user-name">{{ item.author_nickname }}</text>
                            <text class="view-count">{{ item.pageviews }}</text>
                        </view>
                    </view>
                </view>
            </view>

            <view class="content-grid2" v-if="contentList.length > 0 && activeTab == 'follow'">
                <view class="content-card2" v-for="(item, index) in contentList" :key="index" @tap="toDetail(item)">
                    <view class="info-box">
                        <image class="avatar" :src="item.author_headimg" mode="aspectFill"></image>
                        <view class="info-box-text">
                            <text class="name">{{ item.author_nickname }}</text>
                            <text class="fans">{{ item.author_fans }}关注</text>
                        </view>
                    </view>
                </view>
            </view>
            <view class="no-data-message" @tap.stop="goto" :data-url="'/pagesZone/video/video_index'">
                前往首页查看更多视频
            </view>
            <view class="content-tip" v-if="nodata">
                暂无数据
            </view>
            <nomore text="没有更多内容了" v-if="nomore"></nomore>
            <!-- </scroll-view> -->
        </view>
    </view>
</template>

<script>
var app = getApp();
export default {
    data() {
        return {
            activeTab: 'hot',
            activeFilter: 'latest',
            nomore: false,
            nodata: false,
            pagenum: 1,
            limit: 10,
            newContentList: [
            ],
            hotContentList: [
            ],
            markContentList: [
            ],
            followContentList: [
            ]
        }
    },
    computed: {
        // 根据当前选中的标签和筛选条件显示对应的内容列表
        contentList() {
            switch (this.activeTab) {
                case 'new':
                    return this.newContentList
                case 'hot':
                    return this.hotContentList
                case 'mark':
                    return this.markContentList
                case 'follow':
                    return this.followContentList
                default:
                    return this.newContentList
                // 根据筛选条件处理列表
                // switch (this.activeFilter) {
                //     case 'hot':
                //         return this.hotContentList
                //     case 'latest':
                //         return list
                //     case 'replies':
                //         // 这里可以根据回复数排序，这里简单返回原列表
                //         return list.slice().reverse()
                //     default:
                //         return list
            }
        },
        getType() {
            switch (this.activeTab) {
                case 'new':
                    return '1'
                case 'hot':
                    return '2'
                case 'mark':
                    return '3'
                default:
                    return '1'
            }
        }
    },

    onShow() {
        this.getdata();
    },


    mounted: function (opt) {
        this.getdata()
    },
    onPullDownRefresh: function () {
        this.getdata();
    },

    onReachBottom: function () {
        console.log('onReachBottom', this.nodata, this.nomore);
        if (!this.nodata && !this.nomore) {
            this.pagenum = this.pagenum + 1;
            this.getdata();
        }
    },
    methods: {

        getdata() {
            //console.log('tab', this.activeTab, this.nodata, this.nomore);
            app.showLoading('加载中');
            var that = this;
            that.nodata = false;
            that.nomore = false;
            if (that.activeTab == 'new') {
                app.get('VideoCenter/list?type=1', { limit: that.limit, page: that.pagenum }, function (res) {
                    if (res && res.status == 200) {
                        app.showLoading(false);
                        if (that.pagenum == 1) {
                            that.newContentList = res.data;
                            if (res.data.length == 0) {
                                that.nodata = true;
                            }
                        } else {
                            if (res.data.length == 0) {
                                that.nomore = true;

                            } else {
                                var datalist = that.newContentList;
                                var newdata = datalist.concat(res.data);
                                that.newContentList = newdata;
                            }
                        }
                    } else {
                        app.showLoading(false);
                        app.error(res.msg);
                    }
                    that.loaded();
                });
            } else if (that.activeTab == 'hot') {
                app.get('VideoCenter/list?type=2', { limit: that.limit, page: that.pagenum }, function (res) {
                    if (res && res.status == 200) {
                        app.showLoading(false);
                        if (that.pagenum == 1) {
                            that.hotContentList = res.data;
                            if (res.data.length == 0) {
                                that.nodata = true;
                            }
                        } else {
                            if (res.data.length == 0) {
                                that.nomore = true;
                            } else {
                                var datalist = that.hotContentList;
                                var newdata = datalist.concat(res.data);
                                that.hotContentList = newdata;
                            }
                        }
                    } else {
                        app.showLoading(false);
                        app.error(res.msg);
                    }
                    that.loaded();
                });
            } else if (that.activeTab == 'mark') {
                app.get('VideoCenter/collectList', { limit: that.limit, page: that.pagenum }, function (res) {
                    if (res && res.status == 200) {
                        app.showLoading(false);
                        if (that.pagenum == 1) {
                            that.markContentList = res.data;
                            if (res.data.length == 0) {
                                that.nodata = true;
                            }
                        } else {
                            if (res.data.length == 0) {
                                that.nomore = true;
                            } else {
                                var datalist = that.markContentList;
                                var newdata = datalist.concat(res.data);
                                that.markContentList = newdata;
                            }
                        }
                    } else {
                        app.showLoading(false);
                        app.error(res.msg);
                    }
                    that.loaded();
                });
            } else if (that.activeTab == 'follow') {
                app.get('VideoCenter/involvedList', { limit: that.limit, page: that.pagenum }, function (res) {
                    if (res && res.status == 200) {
                        app.showLoading(false);
                        if (that.pagenum == 1) {
                            that.followContentList = res.data;
                            if (res.data.length == 0) {
                                that.nodata = true;
                            }
                        } else {
                            if (res.data.length == 0) {
                                that.nomore = true;
                            } else {
                                var datalist = that.followContentList;
                                var newdata = datalist.concat(res.data);
                                that.followContentList = newdata;
                            }
                        }
                    } else {
                        console.log('res', res);
                        app.showLoading(false);
                        app.error(res.msg);
                    }
                    that.loaded();
                });
            }

        },

        // 切换标签页
        switchTab(tab) {
            this.activeTab = tab
            // 切换标签时重置筛选条件
            this.activeFilter = 'latest'
            this.clearData();
            this.getdata();
        },

        clearData() {
            this.pagenum = 1;
            this.nodata = false;
            this.nomore = false;
            this.newContentList = [];
            this.hotContentList = [];
            this.markContentList = [];
            this.followContentList = [];
        },
        // 切换筛选条件
        switchFilter(filter) {
            this.activeFilter = filter
        },
        toDetail(item) {
            if (this.activeTab == 'mark') {
                uni.navigateTo({
                    url: `/pagesZone/video/video_detail?id=${item.video_center_list_id}&listid=${item.video_center_content_id}`
                })
            } else if (this.activeTab == 'follow') {
                uni.navigateTo({
                    url: `/pagesZone/video/author_detail?id=${item.video_center_author_id}`
                })
            } else {
                uni.navigateTo({
                    url: `/pagesZone/video/video_detail?id=${item.id}`
                })
            }

        }
    }
}
</script>

<style lang="scss">
.life-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: #f5f5f5;


    .search-container {
        width: 100%;
        height: 94rpx;
        // padding: 16rpx 23rpx 14rpx 23rpx;
        position: relative;
        overflow: hidden;
        // border-bottom: 1px solid #f5f5f5;

        .search-box {
            display: flex;
            align-items: center;
            height: 70rpx;
            border-radius: 16rpx;
            border: 0;
            background-color: #f7f7f7;
            flex: 1;
        }

        .search-box .img {
            width: 46rpx;
            height: 44rpx;
            margin-right: 10rpx;
            margin-left: 30rpx
        }

        .search-box .search-text {
            font-size: 24rpx;
            color: #939393;
            width: 100%;
        }

    }



    .fixed-header {
        position: sticky;
        top: 0;
        left: 0;
        right: 0;
        z-index: 100;
        background: #fff;
        padding: 20rpx 20rpx 0;
        box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

        .nav-tabs {
            display: flex;
            margin-bottom: 20rpx;

            .tab-item {
                margin-right: 30rpx;
                font-size: 32rpx;
                color: #666;
                padding: 10rpx 0;

                &.active {
                    color: #333;
                    font-weight: bold;
                    position: relative;

                    .active-curve {
                        position: absolute;
                        left: 50%;
                        margin-left: -24rpx;
                        bottom: 0rpx;
                        height: 3px;
                        border-radius: 1.5px;
                        width: 48rpx
                    }

                    &::after {
                        content: '';
                        position: absolute;
                        bottom: 0;
                        left: 50%;
                        transform: translateX(-50%);
                        width: 100%;
                        height: 4rpx;
                        // background: #ba1f25;
                        border-radius: 2rpx;
                    }
                }
            }
        }

        .filter-options {
            display: flex;
            margin-bottom: 20rpx;
            flex-wrap: nowrap;
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
            padding-bottom: 10rpx;

            &::-webkit-scrollbar {
                display: none;
            }

            .filter-item {
                margin-right: 20rpx;
                padding: 10rpx 20rpx;
                font-size: 28rpx;
                color: #666;
                background: #f5f5f5;
                border-radius: 30rpx;
                white-space: nowrap;

                &.active {
                    color: #fff;
                    background: #ba1f25;
                }
            }
        }
    }

    // .scrollable-content {
    .content-container {
        // flex: 1;
        // overflow-y: auto;
        // -webkit-overflow-scrolling: touch;
        // padding: 20rpx;

        // &::-webkit-scrollbar {
        //     display: none;
        // }

        width: 100%;
        // margin-top: 106rpx;
        font-size: 26rpx;
        padding: 0 14rpx;

        .content-tip {
            text-align: center;
            color: #999;
            font-size: 28rpx;
            padding: 20rpx;
        }

        .content-grid {
            display: flex;
            flex-wrap: wrap;
            margin: 0 -10rpx;

            .content-card {
                width: calc(50% - 20rpx);
                margin: 10rpx;
                background: #fff;
                border-radius: 15rpx;
                overflow: hidden;
                box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);

                .card-image {
                    width: 100%;
                    height: 200rpx;
                }

                .card-info {
                    padding: 20rpx;


                    .card-title {
                        font-size: 28rpx;
                        color: #333;
                        line-height: 1.4;
                        margin-bottom: 10rpx;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        display: block;
                    }

                    .user-info {
                        display: flex;
                        align-items: center;

                        .user-avatar {
                            width: 40rpx;
                            height: 40rpx;
                            border-radius: 50%;
                            margin-right: 10rpx;
                        }

                        .user-name {
                            font-size: 24rpx;
                            color: #666;
                            flex: 1;
                        }

                        .view-count {
                            font-size: 24rpx;
                            color: #999;

                            &::before {
                                content: '';
                                display: inline-block;
                                width: 24rpx;
                                height: 24rpx;
                                background: url('@/pagesSocial/static/img/view.png') no-repeat;
                                background-size: contain;
                                vertical-align: middle;
                                margin-right: 6rpx;
                            }
                        }
                    }
                }
            }
        }

        .content-grid2 {
            display: flex;
            flex-wrap: wrap;
            margin: 0 -10rpx;


            .content-card2 {
                width: 100%;
                margin: 10rpx;
                background: #fff;
                border-radius: 15rpx;
                overflow: hidden;
                box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);

                // .card-image {
                //     width: 100%;
                //     height: 200rpx;
                // }

                .info-box {
                    display: flex;
                    align-items: center;
                    height: 150rpx;
                    margin-left: 20rpx;

                    .avatar {
                        width: 110rpx;
                        height: 110rpx;
                        border-radius: 50%;
                        margin-right: 10rpx;
                    }

                    .info-box-text {
                        display: flex;
                        flex-direction: column;
                        justify-content: center;

                        .name {
                            font-size: 34rpx;
                            color: #666;
                            flex: 1;
                            margin: 10rpx;
                        }

                        .fans {
                            font-size: 29rpx;
                            color: #666;
                            flex: 1;
                            margin: 10rpx;
                        }

                    }
                }
            }
        }
    }

}

.no-data-message {
    text-align: center;
    padding: 20px;
    font-size: 28rpx;
    color: #888;
    background-color: #f7f7f7;
    border-radius: 12rpx;
    margin: 20px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
</style>
